package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import com.updateNew.MyIsif;
import org.come.Frame.PartnerJframe;
import org.come.bean.LoginResult;
import org.come.entity.Pal;
import org.come.model.PalData;
import org.come.until.AnalysisString;
import org.come.until.CutButtonImage;

import com.tool.btn.PartnerBtn;
import com.tool.role.RoleData;
import com.tool.tcp.GetTcpPath;
import com.tool.tcpimg.UIUtils;

/**
 * @author HGC<br>
 * @time 2019年12月20日 下午4:45:24<br>
 * @class 类名:PartnerUnitJpanel 伙伴个人面板<br>
 */
public class PartnerUnitJpanel extends JPanel {

    /**
     * 字段名:labUnitImg 伙伴图片 <br>
     * 字段名:labName 伙伴名字 <br>
     * 字段名:labLvl 伙伴等级<br>
     * 字段名:labAck 伙伴操作<br>
     */
    private JLabel labUnitImg, labName, labLvl, labActivate;
    private PartnerBtn labAck;

    /** 字段名:pid 当前伙伴id<br> */
    private int pid;

    /** 字段名:state 当前伙伴状态 :-1未激活 0激活为参战 1-4参战<br> */
    private int state = -1;

    public PartnerUnitJpanel() {
        this.setPreferredSize(new Dimension(147, 49));
        setOpaque(false);
        setLayout(null);
        getLabActivate();
        getLabUnitImg();
        getLabName();
        getLabLvl();
        getLabAck();
        MListener listener = new MListener(this);
        this.addMouseListener(listener);

    }

    /**
     * @author HGC<br>
     * @time 2019年12月20日 下午6:58:06<br>
     * @class 类名:MListener 监听<br>
     */
    class MListener extends MouseAdapter {
        private PartnerUnitJpanel partnerUnitJpanel;

        public MListener(PartnerUnitJpanel partnerUnitJpanel) {
            super();
            this.partnerUnitJpanel = partnerUnitJpanel;
        }

        @Override
        public void mousePressed(MouseEvent e) {
            PartnerJframe.getPartnerJframe().getPartnerMainJpanel().setPalDataChooseId(partnerUnitJpanel.getPid());
            LoginResult loginResult = RoleData.getRoleData().getLoginResult();
            String pals = loginResult.getPals();
            String lvl = AnalysisString.lvl(loginResult.getGrade());
            List<Pal> palsList = RoleData.getRoleData().getPals();
            if (pals != null) {
                String[] palsArr = pals.split("\\|");
                for (int i = 0; i < palsList.size(); i++) {
                    Pal pal = palsList.get(i);
                    if (pal.getpId() == partnerUnitJpanel.getPid()) {
                        state = changePalState(pal, palsArr,lvl);
                        break;
                    }
                }
            }
            changeWarBtn();
        }
    }

    /**
     * 修改参战按钮
     */
    public void changeWarBtn() {
        String text = "激活";
        if (state == -1) {
        } else if (state == 0) {
            text = "参战";
        } else if (state > 0) {
            text = "调整";
        }
        PartnerJframe.getPartnerJframe().getPartnerMainJpanel().getPartnerCardJpanel().getPartnerTeamJpanel()
                .getBtnWar().setText(text);
    }

    /**
     * 是否激活
     *
     * @param palData
     * @param pal
     */
    public void showPal(PalData palData, Pal pal, String[] palsArr,String lvl) {
        this.pid = palData.getPalId();
        String aaa="-1";

        if(GetTcpPath.STRTMPXJ.equals("2"))  aaa=""; else aaa="-1";
        if (pal == null) {


//伙伴头像
            labUnitImg.setIcon(CutButtonImage.getImage("img/newHead/p" + palData.getSkin() + ".png", 40, 40));
            labName.setText(palData.getName());
            if(lvl!=null)
                labLvl.setText(lvl + "级");
            PartnerJframe.getPartnerJframe().getPartnerMainJpanel().getPartnerCardJpanel().getPartnerTeamJpanel()
                    .getBtnWar().setText("激活");
            state = -1;
        } else {
            labUnitImg.setIcon(CutButtonImage.getImage("img/newHead/p" + palData.getSkin() +  ".png", 40, 40));
            labName.setText(palData.getName());
            if(lvl!=null)
                labLvl.setText(lvl + "级");
            state = changePalState(pal, palsArr,lvl);
        }
    }

    /** 修改伙伴状态 1-4参战 0已激活 */
    public int changePalState(Pal pal, String[] palsArr,String lvl) {
        String style = MyIsif.getStyle();
        if ("水墨UI".equals(style)) {
        if (pal != null) {
            labActivate.setVisible(false);
            if(lvl!=null)
                labLvl.setText(lvl + "级");
            if (palsArr != null) {
                for (int i = 0; i < palsArr.length; i++) {
                    if (palsArr[i].equals(pal.getId() + "")) {
                        labAck.setBtn(1);
                        labAck.setIcon(CutButtonImage.getImage("inkImg/button/2.png", -1, -1));
                        labAck.setText(PartnerJframe.getPartnerJframe().getPartnerMainJpanel().getPartnerCardJpanel()
                                .getPartnerTeamJpanel().getBtnArrStr()[i]);
                        return i + 1;
                    }
                }
            }
            labAck.setBtn(0);
            labAck.setIcon(null);
            labAck.setText(null);
            return 0;
        }
        labAck.setBtn(0);
        labAck.setText(null);
        labActivate.setVisible(true);
        return -1;
        } else {
            if (pal != null) {
                labActivate.setVisible(false);
                if(lvl!=null)
                    labLvl.setText(lvl + "级");
                if (palsArr != null) {
                    for (int i = 0; i < palsArr.length; i++) {
                        if (palsArr[i].equals(pal.getId() + "")) {
                            labAck.setBtn(1);
                            labAck.setIcon(CutButtonImage.getImage("inkImg/hongmu/21_png.button.tab_nex.png", -1, -1));
                            labAck.setText(PartnerJframe.getPartnerJframe().getPartnerMainJpanel().getPartnerCardJpanel()
                                    .getPartnerTeamJpanel().getBtnArrStr()[i]);
                            return i + 1;
                        }
                    }
                }
                labAck.setBtn(0);
                labAck.setIcon(null);
                labAck.setText(null);
                return 0;
            }
            labAck.setBtn(0);
            labAck.setText(null);
            labActivate.setVisible(true);
            return -1;
        }
    }

    /** 刷新 */
    public int changePalState(String[] palsArr) {
        if (palsArr != null) {
            for (int i = 0; i < palsArr.length; i++) {
                if (palsArr[i].equals(pid + "")) {
                    labAck.setBtn(1);
                    labAck.setIcon(CutButtonImage.getImage("inkImg/button/2.png", -1, -1));
                    labAck.setText(PartnerJframe.getPartnerJframe().getPartnerMainJpanel().getPartnerCardJpanel()
                            .getPartnerTeamJpanel().getBtnArrStr()[i]);
                    return i + i;
                }
            }
        } else {
            labAck.setBtn(0);
            labAck.setIcon(null);
            labAck.setText(null);
        }
        return 0;
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        // TODO Auto-generated method stub
        super.paintComponent(g);
        if (icon == null) {
            icon = new ImageIcon("inkImg/background/S153.png");
        }
        g.drawImage(icon.getImage(), 0, 0, 147, 49, this);
    }

    public JLabel getLabUnitImg() {
        if (labUnitImg == null) {
            labUnitImg = new JLabel();
            labUnitImg.setBounds(5, 4, 37, 37);
            this.add(labUnitImg);
        }
        return labUnitImg;
    }

    public void setLabUnitImg(JLabel labUnitImg) {
        this.labUnitImg = labUnitImg;
    }

    public JLabel getLabName() {
        if (labName == null) {
            labName = new JLabel("绿光鹿");
            labName.setForeground(Color.white);
            labName.setFont(UIUtils.TEXT_FONT2);
            labName.setBounds(45, 2, 70, 16);
            this.add(labName);
        }
        return labName;
    }

    public void setLabName(JLabel labName) {
        this.labName = labName;
    }

    public JLabel getLabLvl() {
        if (labLvl == null) {
            labLvl = new JLabel("一转126级");
            labLvl.setForeground(Color.white);
            labLvl.setFont(UIUtils.TEXT_FONT2);
            labLvl.setBounds(45, 22, 70, 16);
            this.add(labLvl);
        }
        return labLvl;
    }

    public void setLabLvl(JLabel labLvl) {
        this.labLvl = labLvl;
    }

    public ImageIcon getIcon() {
        return icon;
    }

    public void setIcon(ImageIcon icon) {
        this.icon = icon;
    }

    public JLabel getLabAck() {
        if (labAck == null) {
            labAck = new PartnerBtn("inkImg/button/2.png", 0, UIUtils.COLOR_BTNTEXT, UIUtils.TEXT_FONT, "", 11);
            labAck.setBounds(100, 10, 34, 17);
            labAck.setForeground(Color.white);
            this.add(labAck);
        }
        return labAck;
    }

    public void setLabAck(PartnerBtn labAck) {
        this.labAck = labAck;
    }

    public int getPid() {
        return pid;
    }

    public void setPid(int pid) {
        this.pid = pid;
    }

    public int getState() {
        return state;
    }

    public void setState(int state) {
        this.state = state;
    }

    public JLabel getLabActivate() {
        if (labActivate == null) {
            labActivate = new JLabel();
            labActivate.setIcon(CutButtonImage.getImage("inkImg/button/B235.png", -1, -1));
            labActivate.setBounds(0, 0, 147, 49);
            this.add(labActivate);
        }
        return labActivate;
    }

    public void setLabActivate(JLabel labActivate) {
        this.labActivate = labActivate;
    }

}
